package com.lalilu.homework3;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.lalilu.homework3.fragment.ChoiceDialogFragment;
import com.lalilu.homework3.fragment.CustomDialogFragment;
import com.lalilu.homework3.fragment.MultiChoiceDialogFragment;
import com.lalilu.homework3.fragment.SimpleDialogFragment;
import com.lalilu.homework3.fragment.SingleChoiceDialogFragment;
import com.lalilu.homework3.utils.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    public static String TAG = MainActivity.class.getName();
    private final List<Dialog> dialogList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setStatusBarColor(this);
        setContentView(R.layout.activity_main);
    }

    private void button1Click() {
        Dialog dialog1 = new AlertDialog.Builder(this)
                .setTitle("旧版对话框")
                .setMessage("这是旧版的对话框，使用简单；但是没有自动支持屏幕选择！")
                .setPositiveButton("确定", (dialog, which) -> Log.i(TAG, "点击了确定"))
                .setNegativeButton("取消", (dialog, which) -> Log.i(TAG, "点击了取消"))
                .create();
        this.dialogList.add(dialog1);
        dialog1.show();
    }

    private void button2Click() {
        SimpleDialogFragment simpleDialog = new SimpleDialogFragment();
        simpleDialog.show(getSupportFragmentManager(), "simpleDialog");
    }

    private void button3Click() {
        ChoiceDialogFragment choiceDialog = new ChoiceDialogFragment();
        choiceDialog.setListener(gender -> Toast.makeText(this, "选择了：" + gender, Toast.LENGTH_SHORT).show());
        choiceDialog.show(getSupportFragmentManager(), "choiceDialog");
    }

    private void button4Click() {
        SingleChoiceDialogFragment singleChoiceDialog = new SingleChoiceDialogFragment();
        singleChoiceDialog.setListener(gender -> Toast.makeText(this, "选择了：" + gender, Toast.LENGTH_SHORT).show());
        singleChoiceDialog.show(getSupportFragmentManager(), "singleChoiceDialog");
    }

    private void button5Click() {
        MultiChoiceDialogFragment multiChoiceDialog = new MultiChoiceDialogFragment();
        multiChoiceDialog.setListener(gender -> Toast.makeText(this, "选择了：" + gender, Toast.LENGTH_SHORT).show());
        multiChoiceDialog.show(getSupportFragmentManager(), "multiChoiceDialog");
    }

    private void button6Click() {
        CustomDialogFragment customDialog = new CustomDialogFragment();
        customDialog.setListener(result -> Toast.makeText(this, "输入结果：\n" + result, Toast.LENGTH_LONG).show());
        customDialog.show(getSupportFragmentManager(), "customDialog");
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        Log.i(TAG, "点击了Button");

        switch (v.getId()) {
            case R.id.button:
                button1Click();
                break;
            case R.id.button2:
                button2Click();
                break;
            case R.id.button3:
                button3Click();
                break;
            case R.id.button4:
                button4Click();
                break;
            case R.id.button5:
                button5Click();
                break;
            case R.id.button6:
                button6Click();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        if (!this.dialogList.isEmpty()) {
            for (Dialog dialog : this.dialogList) {
                if (dialog != null) {
                    dialog.dismiss();
                }
            }
        }
        super.onDestroy();
    }
}